import { httpPost } from "../utils/httpPost";
import { pullDownToRefresh } from "../utils/pullDownToRefresh";
import { useDeviceUuid } from "../utils/useDeviceUuid";
import { useSetting } from "../utils/useSetting";
import { getRoomInfo } from "./getRoomInfo";
import { RedPacketInfo } from "./redPacketInfoVisitor";

export function checkRoomIndex(i: number = 0, start: boolean = true) {
    if (!start) return false;
    let child = idEndsWith('live_explore_feed_card_layout').findOnce()
    while (child == null) {
        child = idEndsWith('live_explore_feed_card_layout').findOnce()
        sleep(1000)
    }
    // 屏幕截图
    const parent = child.parent()
    if (parent) {
        const childCount = parent.childCount()
        if (i === childCount) {
            sleep(1000)
            // 下拉刷新 重新加载数据
            pullDownToRefresh(parent.bounds())
            sleep(3000)
            return true;
        }
        console.log(`正在检查${i}个房间`)
        const child = parent.child(i)
        if (child && child.visibleToUser()) {
            let image: AutoJs.Image = captureScreen()
            const bounds = child.bounds()
            const point = images.findMultiColors(image,
                "#fea37d", [[5, 0, "#fece84"], [11, -5, "#fdee89"], [11, 5, "#feef89"], [15, 0, "#feef89"], [20, 0, "#fec282"], [23, 0, "#d4806c"], [14, 10, "#ff386b"], [11, -11, "#fd386a"], [4, -8, "#fd386a"], [18, -6, "#fd386a"], [5, 11, "#ff386b"], [16, 10, "#ff386b"]],
                {
                    region: [bounds.left, bounds.top, bounds.width(), bounds.height()],
                    threshold: 40,
                    similarity: 0.7
                }
            )
            console.log(`第${i}个房间${point ? "有红包" : "无红包"}`)
            if (point) {
                // 有红包
                const redThread = threads.start(() => {
                    child.click()
                    sleep(3000)
                    // 查看房间信息信息
                    const info = getRoomInfo(start)
                    const share = info.get<any>("share")
                    const redPacketList = info.get<RedPacketInfo[]>('redPacketList') || []
                    if (redPacketList && redPacketList.length > 0 && share) {
                        const postData = {
                            link: share.link,
                            name: share.name,
                            openTime: info.get('openTime') || "",
                            online: info.get('online') || 0,
                            redPacketCount: info.get('redPacketCount') || 0,
                            category: info.get('category') || "",
                            rank: info.get('rank') || "",
                            uuid: useDeviceUuid(),
                            ...(share.share || {}),
                            redPackets: redPacketList.map(red => ({
                                type: red.type,
                                count: red.count,
                                total: red.total,
                                text: red.desc,
                                password: red.password,
                                unit: red.unit,
                                sub_title: red.sub_title,
                                title: red.title,
                                desc: red.desc
                            }))
                        }
                        httpPost("addRedPkg", postData)
                    } else {
                        // 是否是没有账号
                        const setting = useSetting()
                        if (!setting.isTakeRedInfo && share) {
                            httpPost("queueRedPkg", {
                                link: share.link,
                                name: share.name,
                                openTime: info.get('openTime') || "",
                                online: info.get('online') || 0,
                                redPacketCount: info.get('redPacketCount') || 0,
                                category: info.get('category') || "",
                                rank: info.get('rank') || "",
                                uuid: useDeviceUuid(),
                                ...(share.share || {}),
                            }, {})
                            console.log("加入队列")
                        }
                    }
                    sleep(1000)
                    info.clear()
                })
                redThread.waitFor()
                redThread.join()
            }
            sleep(1000)
        }
        return checkRoomIndex(i + 1, start)
    }
}
